package ecoiot.com.testproject.picker;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import java.util.ArrayList;

import ecoiot.com.testproject.R;

/**
 * Created by developer on 17-11-3.
 */

public class TestPhotoPickerActivity extends AppCompatActivity {

    private GridView gridView;
    private ArrayList<Integer> images;
    private ArrayList<Boolean> checked;
    private TextView tvDone;
    private TextView tvSelect;
    private int selectCount = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_picker);
        images = new ArrayList<>();
        checked = new ArrayList<>();
        gridView = (GridView) findViewById(R.id.activity_picker_gv);
        tvDone = (TextView) findViewById(R.id.activity_picker_tv_done);
        tvSelect = (TextView) findViewById(R.id.activity_picker_tv_select);

        initImage();

        tvDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TestPhotoPickerActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        gridView.setAdapter(new PhotoPickerAdapter());
    }

    private void initImage() {
        for (int i = 0; i < 20; i ++) {
            images.add(R.mipmap.background_1);
            checked.add(false);
        }
        tvSelect.setText(String.valueOf(selectCount) + " / " + images.size());
    }

    private class PhotoPickerAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public Object getItem(int position) {
            return images.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            View view;
            if (convertView == null) {
                view = LayoutInflater.from(TestPhotoPickerActivity.this).inflate(R.layout.item_picker_list,null);
                holder = new ViewHolder();
                holder.imageView = (ImageView) view.findViewById(R.id.item_picker_iv);
                holder.checkBox = (CheckBox) view.findViewById(R.id.item_picker_cb);
                view.setTag(holder);
            }else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }

            holder.imageView.setBackgroundResource(R.mipmap.background_1);
            holder.checkBox.setChecked(checked.get(position));

            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isChecked = !holder.checkBox.isChecked();
                    holder.checkBox.setChecked(isChecked);
                    checked.set(position, isChecked);
                    initSelect(isChecked);
                }
            });

            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isChecked = !holder.checkBox.isChecked();
                    holder.checkBox.setChecked(isChecked);
                    checked.set(position, isChecked);
                    initSelect(isChecked);
                }
            });

            int width = (parent.getWidth() - 20) / 3;
            view.setLayoutParams(new GridView.LayoutParams(width,width));

            return view;
        }
    }

    private void initSelect(boolean isChecked) {
        if (isFastDoubleClick()) {
            return;
        }

        if (isChecked) {
            selectCount ++;
        }else {
            selectCount --;
        }

        tvSelect.setText(String.valueOf(selectCount < 0 ? 0 : selectCount) + " / " + images.size());
    }

    private class ViewHolder {
        ImageView imageView;
        CheckBox checkBox;
    }

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeDiff = time - lastClickTime;
        if (timeDiff > 0 && timeDiff < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    private static long lastClickTime;

}
